﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using Forms = System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WordCounter.Core;
using WordCounter.Model;

namespace WordCounter
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainModel Model { get; }

        public MainWindow()
        {
            Model = new MainModel();
            InitializeComponent();
            DataContext = Model;
        }

        /// <summary>
        /// 浏览目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnBrowserClick(object sender, RoutedEventArgs e)
        {
            Forms.FolderBrowserDialog fbd = new Forms.FolderBrowserDialog();
            if(fbd.ShowDialog() == Forms.DialogResult.Cancel)
            {
                return;
            }

            Model.Dir = fbd.SelectedPath.Trim();
        }

        /// <summary>
        /// 执行统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnStaticClick(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty(Model.Dir))
            {
                MessageBox.Show("请选择目录");
                return;
            }

            Model.StatusText = "正在扫描文件";
            Task.Factory.StartNew(() =>
            {
                List<FileItem> items = FileSanner.ScanWordFiles(Model.Dir);
                Dispatcher.Invoke(() =>
                {
                    Model.StatusText = $"扫描到word文档: {items.Count}个";
                });
                return items;
            }).ContinueWith(prev=>
            {
                List<FileItem> items = prev.Result;
                long total = 0;
                for(int i = 0; i < items.Count; i++)
                {
                    FileItem item = items[i];
                    Dispatcher.Invoke(() =>
                    {
                        Model.StatusText = $"正在统计: {i+1}/{items.Count}...";
                    });

                    long page;
                    if (item.Type == FileType.Word)
                    {
                        page = PageCounter.CountWordPage(item.Path);
                    }
                    else
                    {
                        page = PageCounter.CountPDFPage(item.Path);
                    }

                    total += page;

                    FilePageItem fpi = new FilePageItem
                    {
                        Name = item.Name,
                        Path = item.Path,
                        IsDir = item.IsDir,
                        PageCount = page
                    };
                    Dispatcher.Invoke(() => {
                        Model.Items.Add(fpi);
                        Model.TotalPage = total;
                    });
                }

                Dispatcher.Invoke(() =>
                {
                    Model.StatusText = $"统计完毕";
                });

            });
        }
    }
}
